package net.nettytlv.domain;

import java.nio.ByteBuffer;
import java.util.Arrays;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import org.apache.commons.codec.binary.Hex;

@Setter
@Getter
@Builder
public class Packet {

    private byte type;
    private short length;
    private byte[] value;

    public String toHex() {
        ByteBuffer byteBuffer = ByteBuffer.allocate(1 + 2 + this.length);
        byteBuffer.put(this.type);
        byteBuffer.putShort(this.length);
        byteBuffer.put(value);
        return Hex.encodeHexString(byteBuffer);
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("Packet{");
        sb.append("type=").append(type);
        sb.append(", length=").append(length);
        sb.append(", value=").append(Hex.encodeHex(value));
        sb.append('}');
        return sb.toString();
    }
}
